package com.cgsd.heyu.wxapi;

import android.app.Activity;
import android.os.Bundle;

import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.entity.EventMessage;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * Author: sky on 2021/3/31 18:24
 * Email: xcode126@126.com
 * Desc:
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IWXAPI api = WXAPIFactory.createWXAPI(this, AppConfig.WX_ID);
        api.handleIntent(getIntent(), this);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (baseResp.errCode) {
                case 0:
                    ToastHelper.showDefaultToast("支付成功");
                    EventBus.getDefault().post(new EventMessage<>(Constants.EventCode.MY_PUBLISH_REFRESH));
                    finish();
                    break;
                case -1:
                    ToastHelper.showDefaultToast("支付取消");
                    finish();
                    break;
                case -2:
                    ToastHelper.showDefaultToast("请求失败");
                    finish();
                    break;
                default:
                    ToastHelper.showDefaultToast("支付出错");
                    break;
            }
            finish();
        }
    }
}
